#include <QFocusEvent>
#include "mySudoItem.h"

const int FRAMESTYLE = QFrame::Panel;
const Qt::Alignment ALIGNSTYLE = Qt::AlignCenter;
const Qt::FocusPolicy FOCUSPOLICY = Qt::ClickFocus;

mySudoItem::mySudoItem(QWidget *parent)
    : QLabel(parent)
    , m_point(0, 0, 0)
    , m_keyBoard(this)
{
    initConnects();
    this->setFrameStyle(FRAMESTYLE);
    this->setAlignment(ALIGNSTYLE);
    m_keyBoard.hide();
    if (focusProxy() == NULL) {
        this->setFocusProxy(this);
        this->setFocusPolicy(FOCUSPOLICY);
    }
}

mySudoItem::mySudoItem(const myPoint &point, QWidget *parent)
    : QLabel(parent)
    , m_point(point)
    , m_keyBoard(this)
{
    initConnects();
    this->setFrameStyle(FRAMESTYLE);
    this->setAlignment(ALIGNSTYLE);
    m_keyBoard.hide();
    this->setFocusProxy(this);
    this->setFocusPolicy(FOCUSPOLICY);
}

mySudoItem::mySudoItem(int x, int y, int g, QWidget *parent)
    : QLabel(parent)
    , m_point(x, y, g)
    , m_keyBoard(this)
{
    initConnects();
    this->setFrameStyle(FRAMESTYLE);
    this->setAlignment(ALIGNSTYLE);
    m_keyBoard.hide();
    this->setFocusProxy(this);
    this->setFocusPolicy(FOCUSPOLICY);
}

mySudoItem::~mySudoItem()
{
    finalConnects();
}

void mySudoItem::initConnects()
{
    connect(&m_keyBoard, SIGNAL(keyBoardResult(int)), this, SLOT(onKeyBoardResult(int)));
}

void mySudoItem::finalConnects()
{
    disconnect(&m_keyBoard, SIGNAL(keyBoardResult(int)), this, SLOT(onKeyBoardResult(int)));
}

myPoint mySudoItem::getPoint()
{
    return m_point;
}

int mySudoItem::getx()
{
    return m_point.x();
}

int mySudoItem::gety()
{
    return m_point.y();
}

void mySudoItem::setPoint(const myPoint &point)
{
    m_point = point;
}

void mySudoItem::setPoint(int x, int y, int g)
{
    m_point.setX(x);
    m_point.setY(y);
    m_point.setGroup(g);
}

int mySudoItem::getSudoGroup()
{
    return m_point.group();
}

void mySudoItem::focusInEvent(QFocusEvent *ev)
{
    m_keyBoard.show();
    ev->accept();
}

void mySudoItem::focusOutEvent(QFocusEvent *ev)
{
    m_keyBoard.hide();
    ev->accept();
}

void mySudoItem::onClicked()
{
    emit myButtonClicked(m_point);
}

void mySudoItem::onKeyBoardResult(int num)
{
    this->setText(QString("%1").arg(num));
    emit valueChanged(m_point);
}
